package com.kaibes.module.token.core;

import java.time.LocalDateTime;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.kaibes.core.base.excetion.KaibesException;
import com.kaibes.module.token.pojo.Token;
import com.kaibes.module.token.pojo.TokenType;
import com.kaibes.web.util.CookieUtils;

public class TokenUtils {
    
    public static boolean isExpired(Token token) {
        LocalDateTime expiration = token.getExpiration();
        return expiration.compareTo(LocalDateTime.now()) < 0;
    }
    
    public static String getTokenName(String type) {
        TokenType tokenType = TokenType.valueOf(type);
        if (TokenType.WEB == tokenType) {
            return "浏览器";
        } else if (TokenType.PC == tokenType) {
            return "PC客户端";
        } else if (TokenType.MAC == tokenType) {
            return "MAC客户端";
        } else if (TokenType.ANDROID == tokenType) {
            return "安卓客户端";
        } else if (TokenType.IPHONE == tokenType) {
            return "iPhone客户端";
        }
        return "未知设备";
    }
    
    public static TokenType getType(String userAgent) {
        TokenType type = null;
        if (userAgent.startsWith(TokenType.WEB.getValue())) {
            type = TokenType.WEB;
        } else if (userAgent.startsWith(TokenType.PC.getValue())) {
            type = TokenType.PC;
        } else if (userAgent.startsWith(TokenType.MAC.getValue())) {
            type = TokenType.MAC;
        } else if (userAgent.startsWith(TokenType.ANDROID.getValue())) {
            type = TokenType.ANDROID;
        } else if (userAgent.startsWith(TokenType.IPHONE.getValue())) {
            type = TokenType.IPHONE;
        } else if (userAgent.startsWith(TokenType.POSTMAN.getValue())) {
            type = TokenType.POSTMAN;
        }
        return type;
    }
    
    public static void saveToken2Cookie(HttpServletResponse response, String token, int minutes) {
        Cookie cookie = new Cookie("token", token);
        cookie.setHttpOnly(true);
        cookie.setPath("/");
        cookie.setMaxAge(minutes * 60);
        response.addCookie(cookie);
    }
    
    public static void deleteTokenFromCookie(HttpServletRequest request, HttpServletResponse response) {
        try {
            Cookie cookie = CookieUtils.findByName(request, "token");
            cookie.setPath("/");
            cookie.setMaxAge(0);
            response.addCookie(cookie);
        } catch (KaibesException e) {
        }
    }
    
    public static void deleteSecretTokenFromCookie(HttpServletRequest request, HttpServletResponse response) {
        try {
            Cookie cookie = CookieUtils.findByName(request, "secretToken");
            cookie.setPath("/");
            cookie.setMaxAge(0);
            response.addCookie(cookie);
        } catch (KaibesException e) {
        }
    }
    
    public static void deleteAllFromCookie(HttpServletRequest request, HttpServletResponse response) {
        deleteTokenFromCookie(request, response);
        deleteSecretTokenFromCookie(request, response);
    }
    
}
